package com.frxuexi.report

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.NavPath
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.UiState
import com.frxuexi.report.entity.ReportEntity
import com.frxuexi.report.intent.ReportIntent
import com.frxuexi.report.viewmodel.ReportViewModel
import com.google.gson.Gson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.io.Serializable
import java.text.SimpleDateFormat

@Composable
fun ReportPage(
    navHostController: NavHostController,
    vm: ReportViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {
    var list = remember {
        SnapshotStateList<ReportEntity>()
    }

    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess -> {
                        if (it.type == RequestType.DEFAULT) {
                            Log.i("===", it.data.toString())
                            list.addAll(it.data as Collection<ReportEntity>)
                        }
                    }

                    is UiState.onError -> {

                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(ReportIntent.getReports(mapOf(Pair("userId", 1), Pair("page", 1))))
    })

    var isShow by remember {
        mutableStateOf(false)
    }
    Scaffold(floatingActionButton = {
        Icon(
            painter = painterResource(id = R.drawable.icon159),
            contentDescription = "",
            Modifier.clickable {
                isShow = true
            })
    }) {
        Column(Modifier.padding(it)) {
            BaseTitle(navHostController = navHostController, title = "工作日志")
            LazyColumn(content = {
                items(list) {
                    ReportItem(it)
                }
            })
        }
    }

    if (isShow) {
        ReportDialog(navHostController,onDismissRequest = {
            isShow = false
        }){
            isShow = false
        }
    }


}

data class ReportTypeEntity(val type: Int, val name: String, val title: String)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ReportDialog(navHostController: NavHostController,onDismissRequest: () -> Unit,onClose:()->Unit) {
    val list = listOf(
        ReportTypeEntity(1, "1", "日报"),
        ReportTypeEntity(2, "7", "周报"),
        ReportTypeEntity(3, "30", "月报"),
        ReportTypeEntity(4, "季度", "季度报表"),
        ReportTypeEntity(5, "业绩", "业绩报表"),
        ReportTypeEntity(6, "营业", "营业财报")
    )
    AlertDialog(onDismissRequest = { onDismissRequest() }) {
        Column(
            Modifier
                .fillMaxWidth()
                .background(Color.White, RoundedCornerShape(20.dp))
                .padding(10.dp, vertical = 30.dp)
        ) {
            Text(text = "新建汇报", fontWeight = FontWeight.Bold, fontSize = 18.sp)
            LazyVerticalGrid(
                verticalArrangement = Arrangement.Center,
                columns = GridCells.Fixed(3),
                content = {
                    items(list) {
                        DialogItem(entity = it,navHostController,onClose)
                    }
                })
        }
    }
}


@Composable
fun DialogItem(entity: ReportTypeEntity,navHostController: NavHostController,onClose:()->Unit) {

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier.clickable {
            onClose()
            val json = Gson().toJson(entity)
            navHostController.navigate(NavPath.COMMIT_REPORT+"/$json")
        }) {
        Spacer(modifier = Modifier.height(10.dp))
        Column(
            modifier = Modifier
                .size(50.dp)
                .background(
                    colorResource(id = R.color.lightgreen),
                    CircleShape
                )
                .padding(8.dp)
                .background(
                    colorResource(id = R.color.white),
                    RoundedCornerShape(5.dp)
                )
                .border(1.dp, Color.Blue, RoundedCornerShape(5.dp)),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {

            Spacer(modifier = Modifier.height(5.dp))
            Text(
                text = entity.name,
                color = Color.Blue
            )
        }

        Text(text = entity.title)
    }
}

@Composable
fun ReportItem(entity: ReportEntity) {
    Column(
        Modifier.padding(start = 20.dp, top = 10.dp, end = 20.dp),
        verticalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        Row(Modifier.height(40.dp), verticalAlignment = Alignment.CenterVertically) {
            Image(
                modifier = Modifier
                    .size(40.dp)
                    .clip(CircleShape),
                painter = painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = ""
            )
            val type = if (entity.reportType == 1) "日报" else if (entity.reportType == 2) "周报"
            else if (entity.reportType == 3) "月报" else if (entity.reportType == 4) "季度报表"
            else if (entity.reportType == 5) "业绩报表" else "经营财报"

            Spacer(modifier = Modifier.width(10.dp))
            Text(text = type)
        }

        Text(text = entity.reportNowTitle)
        Text(text = entity.reportNowValue)
        Text(text = entity.reportNextTitle)
        Text(text = entity.reportNextValue)

        val time = SimpleDateFormat("yyyy年MM月dd日").format(entity.reportTime)

        Text(text = "$time 发布")
    }
}


